package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


// 收货地址
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    // 查询收货地址列表
    @Override
    public List<Address> findList() {
        // 1.构建条件  select * from address_book where user_id = ?
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId, UserHolder.get().getId());

        // 2.查询并返回
        return addressMapper.selectList(qw);
    }


    //新增地址
    @Override
    public void add(Address address) {
        // 1.补全信息
        address.setUserId(UserHolder.get().getId()); // 用户id
        address.setIsDefault(0); // 非默认地址

        // 2.调用mapper新增
        addressMapper.insert(address);
    }


    // 设置默认收货地址
    @Override
    public void setDefault(Long id) {
        //1.将当前用户所有的默认地址设为0， update address_book set is_default = 0 where user_id = ?
        //1-1 准备修改内容
        Address address = new Address(); //set is_default = 0
        address.setIsDefault(0);

        //1-2 构建修改条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>(); //where id = ?
        qw.eq(Address::getUserId,UserHolder.get().getId());

        //1-3 更新数据库
        addressMapper.update(address,qw);

        //2.将当前前端传递地址id设置为默认收货地址，update address_book set id_default = 1 where id = ?
        Address address1 = new Address();
        address1.setIsDefault(1);
        address1.setId(id);

        addressMapper.updateById(address1);
    }

    // 查询默认收货地址
    @Override
    public Address findDefault() {
        // 1.根据用户id+默认状态查询 select * from address_book where user_id = ? and is_default = 1
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId, UserHolder.get().getId()); // user_id = ?
        qw.eq(Address::getIsDefault, 1); // is_default = 1

        // 2.调用mapper查询
        return addressMapper.selectOne(qw);
    }


    //回显收货地址
    @Override
    public Address findById(Long id) {
        //根据id查询收货地址
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getId,id);
        return addressMapper.selectOne(qw);
    }

    //修改收货地址
    @Override
    public void update(Address address) {
        addressMapper.updateById(address);
        //删除旧的
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getId,address.getId());

        addressMapper.delete(qw);
        //添加新的
        addressMapper.insert(address);
    }

}
